<template>
  <view>
    <view v-if="listData.length > 0" class="grid-body mt-30">
      <view v-for="(item, index) in listData" :key="index" class="check grid-common">
        <view class="title flex">
          {{ item.managerTypeLabel }}
        </view>
        <view class="flex text">
          编号：
          <text>{{ item.workCode }}</text>
        </view>
        <view class="flex text">
          时间：
          <text>{{ item.workTime }}</text>
        </view>
        <view class="flex text">
          检查人：
          <text>{{ item.inspectPersonName }}</text>
        </view>
        <view class="flex text">
          所属单位：
          <text>{{ item.deptName }}</text>
        </view>
        <view class="flex text bor-top justify-between">
          <view class="flex" style="margin-left: auto">
            <view class="flex">
              <button class="info-btn" @click="detail(item)">详情</button>
            </view>
            <view class="flex" v-if="item.personType === bscDockWorkPersonType.CONFIRMED">
              <button class="info-btn" @click="confirm(item)">确认</button>
            </view>
            <view class="flex" v-if="item.personType === bscDockWorkPersonType.DRIVER">
              <button class="info-btn" @click="safetyConfirm(item)">驾驶员安全教育</button>
            </view>
            <view class="flex" v-if="item.personType === bscDockWorkPersonType.SUPERCARGO">
              <button class="info-btn" @click="safetyConfirm(item)">押运员安全教育</button>
            </view>
            <view class="flex" v-if="item.personType === bscDockWorkPersonType.REVIEWED">
              <button class="info-btn" @click="approve(item)">审批</button>
            </view>
            <view class="flex" v-if="item.personType === bscDockWorkPersonType.INSPECTED">
              <button class="info-btn" @click="cacel(item)">取消</button>
            </view>
            <view class="flex" v-if="item.personType === bscDockWorkPersonType.INSPECTED">
              <button class="info-btn" @click="update(item)">修改</button>
            </view>
            <view class="flex" v-if="item.personType === bscDockWorkPersonType.RECORD">
              <button class="info-btn" @click="noteRealLoad(item)">记录实载重量</button>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view v-else>
      <Empty></Empty>
    </view>
  </view>
</template>
<script>
import { getToDoTask, workCancel } from '@/api/dock/trucks'
import Empty from '@/components/cu-empty/index.vue'
import { bscDockWorkPersonType } from '@/utils/constant.js'
export default {
  components: {
    Empty
  },
  data() {
    return {
      bscDockWorkPersonType,
      listData: [],
      pageNum: 1,
      pageSize: 10,
      total: null,
      isFinished: false
    }
  },
  onShow() {
    this.initData()
  },
  onPullDownRefresh() {
    this.initData()
  },
  onReachBottom() {
    this.pageNum++
    this.getList()
  },
  onNavigationBarButtonTap() {
    uni.navigateTo({
      url: `/pages/work/add-load-operation/record`
    })
  },
  methods: {
    initData() {
      this.listData = []
      this.isFinished = false
      this.pageNum = 1
      this.pageSize = 10
      this.total = null
      this.getList()
    },
    getList() {
      if (this.isFinished) {
        return
      }
      const data = {
        pageSize: 10,
        pageNum: this.pageNum
      }
      if (!this.total || this.total > this.listData.length) {
        getToDoTask(data)
          .then((res) => {
            if (this.pageNum === 1) {
              this.listData = res.rows
            } else {
              this.listData.push(...res.rows)
            }
            this.total = res.total
          })
          .finally(() => {
            uni.stopPullDownRefresh()
          })
      }
    },
    confirm(item) {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/task/confirm?subWorkId=` + item.subWorkId,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {}
        }
      })
    },
    safetyConfirm(item) {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/task/educationConfirm?subWorkId=` + item.subWorkId,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {}
        }
      })
    },
    detail(item) {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/detail?subWorkId=` + item.subWorkId,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {}
        }
      })
    },
    approve(item) {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/task/approve?subWorkId=` + item.subWorkId,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {}
        }
      })
    },
    cacel(item) {
      uni.showModal({
        content: '是否取消',
        confirmText: '确定',
        success: async (res) => {
          if (res.confirm) {
            await workCancel(item.subWorkId)
            uni.showToast({
              title: '取消成功！',
              icon: 'success'
            })
            let that = this
            setTimeout(() => {
              that.initData()
            }, 1000)
          }
        }
      })
    },
    update(item) {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/index?id=${item.subWorkId}`
      })
    },
    noteRealLoad(item) {
      uni.navigateTo({
        url: `/pages/work/add-load-operation/task/realLoad?subWorkId=` + item.subWorkId,
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          'callback:success': (data) => {}
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.grid-body {
  background-color: transparent;
}

.grid-common {
  margin-bottom: 30rpx;
  background-color: #fff;
  border-radius: 20rpx;
}

.items-center {
  align-items: center;
}

.info-btn {
  flex-shrink: 0;
  margin-right: 0px;
  margin-left: 10px;
}

.check {
  position: relative;

  .title {
    font-size: 34rpx;
    font-weight: 700;
  }

  .icon-style {
    width: 52rpx;
    height: 52rpx;
    margin-right: 20rpx;
  }

  .text {
    color: #999;

    text {
      width: 76%;
      color: #333;
      display: inline-block;
    }
  }

  .flex-text {
    padding-top: 24rpx;
    display: flex;
    color: #999;

    text {
      color: #333;
      display: inline-block;
    }
  }

  .bor-top {
    padding-top: 20rpx;
    border-top: 1rpx solid #d5d7e3;
  }

  .check-right-top {
    position: absolute;
    right: 20rpx;
    top: 20rpx;

    image {
      width: 100rpx;
      height: 88rpx;
    }
  }
}
</style>
